<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../vue.js"></script>
</head>

<body>
    <button @click="cname = 'one'" :class="{active: cname =='one'}">one</button>
    <button @click="cname = 'two'" :class="{active: cname =='two'}">two</button>
    <button @click="cname = 'three'" :class="{active: cname =='three'}">three</button>

    <div>
        <!-- <one></one> -->
        <keep-alive>
            <component :is="{{cname}}"></component>
        </keep-alive>
    </div>

</body>
<script>
    Vue.component("one", {
        template: `
            <div>
                <h1>one</h1>
            </div>
        `,
        data() {
            return {
                username: ''
            }
        },
        created() {
            fetch("./1.json")
                .then(res => res.json())
                .then(res => {
                    console.log(res)
                    this.username = res.name
                })
        }
    })
    Vue.component("two", {
        template: `
            <div>
                <h1>two</h1>
            </div>
        `
    })
    Vue.component("three", {
        template: `
            <div>
                <h1>three</h1>
            </div>
        `
    })
    new Vue({
        el: "#app",
        data() {
            cname: "one"
        }
    })
</script>

</html>
<style>
    .active {
        color: 'deepskyblue'
    }
</style>